
import { resourceApi } from '@/api/index'
import { treeDataTranslate } from "@/util/index";
const resource = {
    namespaced: true,
    state: {
        resources: {
            list: []
        },
        resource: {
            id: 0,
            parentId: 0,
            name: '',
            url: '',
            tag: '',
            type: 0,
            icon: '',
            status: 1,
            sort: 0,
        }
    },
    getters: {
        resourceList(state) {
            return treeDataTranslate(state.resources.list);
        },
        resourceAll(state) {
            return [{
                id: 0,
                parentId: 0,
                name: '根目录',
                children: treeDataTranslate(state.resources.list)
            }]
        },
    },
    mutations: {
        SET_RESOURCES(state, { list }) {
            state.resources.list = list
        },
        SET_RESOURCE(state, { id, parentId, name, url, tag, type, icon, status, sort }) {
            state.resource.id = id
            state.resource.parentId = parentId
            state.resource.name = name
            state.resource.url = url
            state.resource.tag = tag
            state.resource.type = type
            state.resource.icon = icon
            state.resource.status = status
            state.resource.sort = sort
        }
    },
    actions: {
        GET_RESOURCES({ commit }, param) {
            return new Promise((resolve, reject) => {
                resourceApi.selectList(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        commit('SET_RESOURCES', data)
                        resolve(message)
                    } else {
                        commit('SET_RESOURCES', [])
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })

        },
        GET_RESOURCE_BY_ID({ commit }, id) {
            return new Promise((resolve, reject) => {
                resourceApi.selectById(id).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        commit('SET_RESOURCE', data)
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        ADD_RESOURCE({ commit }, param) {
            return new Promise((resolve, reject) => {
                resourceApi.add(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        UPDATE_RESOURCE({ commit }, param) {
            return new Promise((resolve, reject) => {
                resourceApi.update(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        DELETE_RESOURCE_BY_IDS({ commit }, ids) {
            return new Promise((resolve, reject) => {
                resourceApi.deleteBatch(ids).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default resource